const app = getApp();
const utils = require('../../utils/util.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    phonenumber: null
  },

  /**
   * 组件的初始数据
   */
  data: {
    jhDataForTabbar: {},
    mobile: ""
  },

  ready: function () {
    let mobile = wx.getStorageSync("mobile");
    if (mobile){
      this.setData({
        mobile: mobile
      })
    }
    this.getTabBarPages();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getTabBarPages: function () {
      var that = this;
      if (!wx.getStorageSync('tabBarObj')) {
        console.log("取数据")
        wx.request({
          url: app.globalData.host + '/api/WeApp/GetStoreAppTabBarPages?storeAppId=' + app.globalData.storeAppId,
          success: function (res) {
            if (res.data.Code == 0) {
              wx.setStorageSync('tabBarObj', res.data.Data);
            }
          },
        })
        setTimeout(function () {
          this.getTabBarPages()
        }.bind(that), 1000)
      } else {
        console.log("取缓存")
        that.getStoreAppTabBarPages();
      }
    },
    // 单击tab触发的函数
    onTabbarItemTap(ev) {
      let key = ev.currentTarget.dataset.key;
      let toUrl = "/" + ev.currentTarget.dataset.tourl;
      utils.subscribe();
      wx.reLaunch({
        url: toUrl,
      })
    },

    //获取页面配置
    getStoreAppTabBarPages: function () {
      var that = this;
      var pages = getCurrentPages();
      var url = pages[pages.length - 1].route;//获取当前页路径
      var jhDataForTabbar = wx.getStorageSync('tabBarObj');//底部导航配置
      if (!jhDataForTabbar) {
        return
      }
      var tablist = jhDataForTabbar.list;
      for (var j = 0; j < tablist.length; j++) {
        if (tablist[j].pagePath == url) {//判断当前页面是否设置为导航
          if (tablist[j].pagePath != "pages/index/index"){
            console.log(tablist[j].text)
            utils.aiCardActionRecord(23, JSON.stringify(tablist[j].text));
          }
          for (var i = 0; i < tablist.length; i++) {
            tablist[i].iCount = 0;
            tablist[i].color = jhDataForTabbar.color;
            tablist[i].selectedColor = jhDataForTabbar.selectedColor;
            tablist[i].showColor = jhDataForTabbar.color;
            tablist[i].showImg = tablist[i].iconPath;
          }
          tablist.forEach((item) => {
            if (item.pagePath == url) {
              item.showColor = item.selectedColor;
              item.showImg = item.selectedIconPath;
            } else {
              item.showImg = item.iconPath;
            }
          });
          that.setData({
            jhDataForTabbar: jhDataForTabbar
          })
          break;
        } else {
          that.setData({
            jhDataForTabbar: {}
          })
        }
      }
    },
    //获取手机号
    getPhoneNumber: function (e) {
      console.log(e)
      wx.redirectTo({
        url: '/pages/member/index',
      })
      var that = this;
      wx.login({
        success: function (res) {
          var code = res.code;
          wx.request({
            url: app.globalData.host + '/api/account/getphonenumber?encryptedData=' + encodeURIComponent(e.detail.encryptedData) + '&iv=' + encodeURIComponent(e.detail.iv) + '&storeid=' + app.globalData.storeId + '&storeAppId=' + app.globalData.storeAppId + "&code=" + code,
            success: function (res) {
              if (res.data.Code == 0) {
                app.globalData.userInfo.Mobile = res.data.Data;
                wx.setStorageSync("mobile", res.data.Data)
                that.setData({
                  mobile: res.data.Data
                })
              }
            },
          })
        }
      })
    },
    //消息记录
    formSubmitAuthori: function (e) {
      var formId = e.detail.formId;
      utils.addFormIdMsg(formId);
      // utils.subscribe();
    },
  }
})
